/**
 *****************************************************
 * @sourceName : Etc001l_p00Command.java
 *----------------------------------------------------
 * DESCRIPTION : 성과관리시스템  커맨드
 *****************************************************
 * DATE            AUTHOR           HISTORY
 *----------------------------------------------------
 *2007/06/12     (주)열음소프트       최 초 작 성
 *****************************************************
 */

package command;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;

import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CookieUtils;
import common.Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 * 
 * @author (주)열음소프트
 * @version 1.0
 */
public class LoginCommand extends Command {

	private String next;
	
	public LoginCommand(String next) {
		this.next=next; 
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap userMap = new HashMap();
		Cookie[] cookies=req.getCookies();

		Connection con = null;  
		try {
			
			HttpSession session = req.getSession(true);
			String ent_id = (String)session.getAttribute("ent_id");
			
			if (ent_id==null ){
				throw new CommandException("This is not available company!");
			}
			
			String login_id=Crypto.EncodeBySType(req.getParameter("id"));
			String login_pwd=Crypto.EncodeBySType(req.getParameter("pwd"));
			logger.debug(ent_id+":"+login_id+":"+login_pwd);
			if (login_id!=null && login_pwd!=null){
				con = (new CommonConnect()).getConnection_ds1();
				String[] param=null;
				String queryStr = QueryManager.getQuery("login.query");
				param= new String[3];
				param[0] = login_id;
				param[1] = login_pwd;
				param[2] = ent_id;
				list = DAO.select(con,queryStr,param);
				if (list==null || list.size()==0){
					req.setAttribute("login_message","ID is not member or Password is wrong!");
					//throw new CommandException("ID is not member or Password is wrong!");
				}else{
					userMap =(HashMap)list.get(0);
					UserSession userSession = (UserSession) new UserSession( (Map) userMap );
		            session.setAttribute( "user", userSession );
		            String user_ip   = req.getRemoteAddr();
			        		        
		            String cross_id = Crypto.EncodeBySType2((String)userMap.get( "login_id" ),user_ip);
		            Cookie cook=new Cookie( "usid", cross_id);
		          
//System.out.println("==================>>>>>cross_id:"+cross_id);				       
		           
		            cook.setMaxAge(-1);
		            //logger.debug(cook.getName()+":"+cook.getValue());
		            req.setAttribute("LoginCook", cook);
//		            req.setAttribute("LoginCookIp", cook_ip);
				}
			}

// 리퀘스트에 모두 담는다.
//			req.setAttribute("list_map",listMap);

//로그를 저장한다. 필요에 따라서 사용하며 세션이 있을 경우만 쓴다.			
//			String work_text="";//무슨일을 하는지 저장하고 싶은 내용을 적을 것;
//			saveLog(con,userId,work_text);			

		}catch (Exception ex) {
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
